<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="湖泊名称" prop="lakeId">
        <el-input
          v-model="queryParams.lakeId"
          placeholder="请输入湖泊名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="站点名称" prop="locationDescription">
        <el-input
          v-model="queryParams.locationDescription"
          placeholder="请输入监测站名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        <el-button type="primary" @click="$router.push({ path: '/hma/stationmap' })">跳转地图</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['hma:stations:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['hma:stations:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['hma:stations:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['hma:stations:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="stationsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="监测站ID" align="center" prop="stationId" />
      <el-table-column label="湖泊编号" align="center" prop="lakeId" />
      <el-table-column label="监测站位置描述" align="center" prop="locationDescription" />
      <el-table-column label="纬度坐标" align="center" prop="latitude" />
      <el-table-column label="经度坐标" align="center" prop="longitude" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['hma:stations:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['hma:stations:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改监测站点对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="stationsRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="关联湖泊" prop="lakeId">
          <el-input v-model="form.lakeId" placeholder="请输入关联lake_id" />
        </el-form-item>
        <el-form-item label="位置描述" prop="locationDescription">
          <el-input v-model="form.locationDescription" placeholder="请输入监测站位置描述" />
        </el-form-item>
        <el-form-item label="纬度坐标" prop="latitude">
          <el-input v-model="form.latitude" placeholder="请输入纬度坐标" />
        </el-form-item>
        <el-form-item label="经度坐标" prop="longitude">
          <el-input v-model="form.longitude" placeholder="请输入经度坐标" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-divider content-position="center">污染物监测信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="Plus" @click="handleAddHmaPollutant">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="Delete" @click="handleDeleteHmaPollutant">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="hmaPollutantList" :row-class-name="rowHmaPollutantIndex" @selection-change="handleHmaPollutantSelectionChange" ref="hmaPollutant">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="锑 (Sb) 浓度 (mg/L), 限量 &lt; 0.005 mg/L" prop="seleniumSelenium" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.seleniumSelenium" placeholder="请输入锑 (Sb) 浓度 (mg/L), 限量 &lt; 0.005 mg/L" />
            </template>
          </el-table-column>
          <el-table-column label="砷 (As) 浓度 (mg/L), 限量 &lt; 0.01 mg/L" prop="arsenic" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.arsenic" placeholder="请输入砷 (As) 浓度 (mg/L), 限量 &lt; 0.01 mg/L" />
            </template>
          </el-table-column>
          <el-table-column label="铜 (Cu) 浓度 (mg/L), 限量 &lt; 1.0 mg/L" prop="copper" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.copper" placeholder="请输入铜 (Cu) 浓度 (mg/L), 限量 &lt; 1.0 mg/L" />
            </template>
          </el-table-column>
          <el-table-column label="钡 (Ba) 浓度 (mg/L), 限量 &lt; 0.7 mg/L" prop="barium" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.barium" placeholder="请输入钡 (Ba) 浓度 (mg/L), 限量 &lt; 0.7 mg/L" />
            </template>
          </el-table-column>
          <el-table-column label="镉 (Cd) 浓度 (mg/L), 限量 &lt; 0.003 mg/L" prop="cadmium" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.cadmium" placeholder="请输入镉 (Cd) 浓度 (mg/L), 限量 &lt; 0.003 mg/L" />
            </template>
          </el-table-column>
          <el-table-column label="铬 (Cr) 浓度 (mg/L), 限量 &lt; 0.05 mg/L" prop="chromium" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.chromium" placeholder="请输入铬 (Cr) 浓度 (mg/L), 限量 &lt; 0.05 mg/L" />
            </template>
          </el-table-column>
          <el-table-column label="铅 (Pb) 浓度 (mg/L), 限量 &lt; 0.01 mg/L" prop="plumbum" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.plumbum" placeholder="请输入铅 (Pb) 浓度 (mg/L), 限量 &lt; 0.01 mg/L" />
            </template>
          </el-table-column>
          <el-table-column label="汞 (Hg) 浓度 (mg/L), 限量 &lt; 0.001 mg/L" prop="mercury" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.mercury" placeholder="请输入汞 (Hg) 浓度 (mg/L), 限量 &lt; 0.001 mg/L" />
            </template>
          </el-table-column>
          <el-table-column label="锰 (Mn) 浓度 (mg/L), 限量 &lt; 0.4 mg/L" prop="manganese" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.manganese" placeholder="请输入锰 (Mn) 浓度 (mg/L), 限量 &lt; 0.4 mg/L" />
            </template>
          </el-table-column>
          <el-table-column label="镍 (Ni) 浓度 (mg/L), 限量 &lt; 0.02 mg/L" prop="nickel" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.nickel" placeholder="请输入镍 (Ni) 浓度 (mg/L), 限量 &lt; 0.02 mg/L" />
            </template>
          </el-table-column>
          <el-table-column label="银 (Ag) 浓度 (mg/L), 限量 &lt; 0.05 mg/L" prop="silver" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.silver" placeholder="请输入银 (Ag) 浓度 (mg/L), 限量 &lt; 0.05 mg/L" />
            </template>
          </el-table-column>
          <el-table-column label="硼酸盐 (以 B 计) 浓度 (mg/L), 限量 &lt; 5 mg/L" prop="borate" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.borate" placeholder="请输入硼酸盐 (以 B 计) 浓度 (mg/L), 限量 &lt; 5 mg/L" />
            </template>
          </el-table-column>
          <el-table-column label="硝酸盐 (以 NO3- 计) 浓度 (mg/L), 限量 &lt; 45 mg/L" prop="nitrate" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.nitrate" placeholder="请输入硝酸盐 (以 NO3- 计) 浓度 (mg/L), 限量 &lt; 45 mg/L" />
            </template>
          </el-table-column>
          <el-table-column label="氟化物 (以 F- 计) 浓度 (mg/L), 限量 &lt; 1.5 mg/L" prop="fluoride" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.fluoride" placeholder="请输入氟化物 (以 F- 计) 浓度 (mg/L), 限量 &lt; 1.5 mg/L" />
            </template>
          </el-table-column>
          <el-table-column label="耗氧量 (以 O2 计) 浓度 (mg/L), 限量 &lt; 3.0 mg/L" prop="oxygenDemand" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.oxygenDemand" placeholder="请输入耗氧量 (以 O2 计) 浓度 (mg/L), 限量 &lt; 3.0 mg/L" />
            </template>
          </el-table-column>
          <el-table-column label="挥发酚 (以苯酚计) 浓度 (mg/L), 限量 &lt; 0.002 mg/L" prop="volatilePhenol" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.volatilePhenol" placeholder="请输入挥发酚 (以苯酚计) 浓度 (mg/L), 限量 &lt; 0.002 mg/L" />
            </template>
          </el-table-column>
          <el-table-column label="氰化物 (以 CN- 计) 浓度 (mg/L), 限量 &lt; 0.01 mg/L" prop="cyanide" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.cyanide" placeholder="请输入氰化物 (以 CN- 计) 浓度 (mg/L), 限量 &lt; 0.01 mg/L" />
            </template>
          </el-table-column>
          <el-table-column label="阴离子合成洗涤剂浓度 (mg/L), 限量 &lt; 0.3 mg/L" prop="anionicDetergent" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.anionicDetergent" placeholder="请输入阴离子合成洗涤剂浓度 (mg/L), 限量 &lt; 0.3 mg/L" />
            </template>
          </el-table-column>
          <el-table-column label="矿物油浓度 (mg/L), 限量 &lt; 0.05 mg/L" prop="mineralOil" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.mineralOil" placeholder="请输入矿物油浓度 (mg/L), 限量 &lt; 0.05 mg/L" />
            </template>
          </el-table-column>
          <el-table-column label="亚硝酸盐 (以 NO2- 计) 浓度 (mg/L), 限量 &lt; 0.1 mg/L" prop="nitrite" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.nitrite" placeholder="请输入亚硝酸盐 (以 NO2- 计) 浓度 (mg/L), 限量 &lt; 0.1 mg/L" />
            </template>
          </el-table-column>
          <el-table-column label="溴酸盐浓度 (mg/L), 限量 &lt; 0.01 mg/L" prop="bromate" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.bromate" placeholder="请输入溴酸盐浓度 (mg/L), 限量 &lt; 0.01 mg/L" />
            </template>
          </el-table-column>
          <el-table-column label="总放射性浓度 (Bq/L), 限量 &lt; 1.5 Bq/L" prop="totalRadioactivity" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.totalRadioactivity" placeholder="请输入总放射性浓度 (Bq/L), 限量 &lt; 1.5 Bq/L" />
            </template>
          </el-table-column>
          <el-table-column label="226镭放射性浓度 (Bq/L), 限量 &lt; 1.1 Bq/L" prop="radium226Radioactivity" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.radium226Radioactivity" placeholder="请输入226镭放射性浓度 (Bq/L), 限量 &lt; 1.1 Bq/L" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Stations">
import { listStations, getStations, delStations, addStations, updateStations } from "@/api/hma/stations";

const { proxy } = getCurrentInstance();

const stationsList = ref([]);
const hmaPollutantList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const checkedHmaPollutant = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    lakeId: null,
    locationDescription: null,
  },
  rules: {
    lakeId: [
      { required: true, message: "关联到 hma_lakes 表中的 lake_id不能为空", trigger: "blur" }
    ],
    locationDescription: [
      { required: true, message: "监测站位置描述不能为空", trigger: "blur" }
    ],
    latitude: [
      { required: true, message: "纬度坐标不能为空", trigger: "blur" }
    ],
    longitude: [
      { required: true, message: "经度坐标不能为空", trigger: "blur" }
    ],
    createBy: [
      { required: true, message: "创建人不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询监测站点列表 */
function getList() {
  loading.value = true;
  listStations(queryParams.value).then(response => {
    stationsList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    stationId: null,
    lakeId: null,
    locationDescription: null,
    latitude: null,
    longitude: null,
    createTime: null,
    updateTime: null,
    createBy: null,
    updateBy: null,
    remark: null
  };
  hmaPollutantList.value = [];
  proxy.resetForm("stationsRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.stationId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加监测站点";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _stationId = row.stationId || ids.value
  getStations(_stationId).then(response => {
    form.value = response.data;
    hmaPollutantList.value = response.data.hmaPollutantList;
    open.value = true;
    title.value = "修改监测站点";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["stationsRef"].validate(valid => {
    if (valid) {
      form.value.hmaPollutantList = hmaPollutantList.value;
      if (form.value.stationId != null) {
        updateStations(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addStations(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _stationIds = row.stationId || ids.value;
  proxy.$modal.confirm('是否确认删除监测站点编号为"' + _stationIds + '"的数据项？').then(function() {
    return delStations(_stationIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 污染物监测序号 */
function rowHmaPollutantIndex({ row, rowIndex }) {
  row.index = rowIndex + 1;
}

/** 污染物监测添加按钮操作 */
function handleAddHmaPollutant() {
  let obj = {};
  obj.seleniumSelenium = "";
  obj.arsenic = "";
  obj.copper = "";
  obj.barium = "";
  obj.cadmium = "";
  obj.chromium = "";
  obj.plumbum = "";
  obj.mercury = "";
  obj.manganese = "";
  obj.nickel = "";
  obj.silver = "";
  obj.borate = "";
  obj.nitrate = "";
  obj.fluoride = "";
  obj.oxygenDemand = "";
  obj.volatilePhenol = "";
  obj.cyanide = "";
  obj.anionicDetergent = "";
  obj.mineralOil = "";
  obj.nitrite = "";
  obj.bromate = "";
  obj.totalRadioactivity = "";
  obj.radium226Radioactivity = "";
  obj.remark = "";
  hmaPollutantList.value.push(obj);
}

/** 污染物监测删除按钮操作 */
function handleDeleteHmaPollutant() {
  if (checkedHmaPollutant.value.length == 0) {
    proxy.$modal.msgError("请先选择要删除的污染物监测数据");
  } else {
    const hmaPollutants = hmaPollutantList.value;
    const checkedHmaPollutants = checkedHmaPollutant.value;
    hmaPollutantList.value = hmaPollutants.filter(function(item) {
      return checkedHmaPollutants.indexOf(item.index) == -1
    });
  }
}

/** 复选框选中数据 */
function handleHmaPollutantSelectionChange(selection) {
  checkedHmaPollutant.value = selection.map(item => item.index)
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('hma/stations/export', {
    ...queryParams.value
  }, `stations_${new Date().getTime()}.xlsx`)
}

getList();
</script>
